package com.dfrz.bookstore.dao.impl;

import com.dfrz.bookstore.dao.Dao;
import com.dfrz.bookstore.entry.Order;
import com.dfrz.bookstore.entry.OrderItem;
import com.dfrz.bookstore.util.JdbcUtils;

import java.sql.*;
import java.util.ArrayList;
import java.util.List;

/**
 * 类功能描述:
 *
 * @Author keyan
 * @Date 2021/5/20 23:16
 */
public class OrderItemDaoImpl implements Dao<OrderItem> {
    @Override
    public boolean insert(OrderItem orderItem) {
        String sql = "insert into order_item (user_id,book_id,order_id,price_sum) values (?,?,?,?)";
        PreparedStatement prep = null;
        Connection conn = null;
        try {
            conn = JdbcUtils.getConnection();
            prep = conn.prepareStatement(sql);
            prep.setInt(1,orderItem.getUser_id());
            prep.setInt(2,orderItem.getBook_id());
            prep.setInt(3,orderItem.getOrder_id());
            prep.setDouble(4,orderItem.getPrice_sum());

            int i = prep.executeUpdate();
            if (i>0){
                return true;
            }

        } catch (SQLException e) {
            e.printStackTrace();
        }finally {
            try {
                JdbcUtils.close(conn,prep,null);
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
        return false;
    }

    @Override
    public boolean deleteObjById(int i) {
        String sql = "delete from order_item where id = "+i ;
        Connection conn = null;
        Statement stat;
        try {
            conn = JdbcUtils.getConnection();
            stat = conn.createStatement();
            int flag = stat.executeUpdate(sql);
            if (flag>0) {
                return true;
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return false;

    }

    @Override
    public boolean updateByObj(OrderItem orderItem) {
        if (orderItem.getId()==0){
            return false;
        }
        StringBuilder sb = new StringBuilder("update order_item set id = "+orderItem.getId());
//        if (orderItem.getUser_id()!=0){
//            sb.append(" , user_id = '"+orderItem.getUser_id()+"' ");
//        }
        if (orderItem.getBook_id()!=0){
            sb.append(" , book_id = "+orderItem.getBook_id());
        }
//        if (orderItem.getOrder_id()!=0){
//            sb.append(" , order_id = "+orderItem.getOrder_id());
//        }
        if (orderItem.getPrice_sum()!=0){
            sb.append(" , price_sum = "+orderItem.getPrice_sum());
        }
        sb.append(" where id = "+ orderItem.getId() );
        Connection conn = null;
        Statement stat = null;
        try {
            conn = JdbcUtils.getConnection();
            stat =  conn.createStatement();
            int i = stat.executeUpdate(sb.toString());
            if (i>0){
                return true;
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }finally {
            try {
                JdbcUtils.close(conn,stat,null);
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
        return false;
    }

    @Override
    public List<OrderItem> findObjByObj(OrderItem orderItem) {
        List<OrderItem> orderItems = new ArrayList<>();
        StringBuilder sb = new StringBuilder("select * from order_item where 1=1 ");
        if (orderItem.getId()!= 0){
            sb.append(" and id = "+orderItem.getId());
        }
        if (orderItem.getUser_id()!=0){
            sb.append(" and user_id = "+orderItem.getUser_id());
        }
        if (orderItem.getBook_id()!=0){
            sb.append(" and book_id = "+orderItem.getBook_id());
        }
        if (orderItem.getPrice_sum()!=0){
            sb.append(" and price_sum = "+orderItem.getPrice_sum());
        }
        if (orderItem.getOrder_id()!=0){
            sb.append(" and order_id = "+orderItem.getOrder_id());
        }
        Connection conn = null;
        Statement stat = null;
        ResultSet resultSet =null;
        try {
            conn= JdbcUtils.getConnection();
            stat = conn.createStatement();
            resultSet = stat.executeQuery(sb.toString());

            while (resultSet.next()){
                OrderItem orderItem1 = new OrderItem();
                orderItem1.setId(resultSet.getInt("id"));
                orderItem1.setUser_id(resultSet.getInt("user_id"));
                orderItem1.setBook_id(resultSet.getInt("book_id"));
                orderItem1.setOrder_id(resultSet.getInt("order_id"));
                orderItem1.setPrice_sum(resultSet.getDouble("price_sum"));
                orderItem1.setCreate_time(resultSet.getDate("create_time"));
                orderItems.add(orderItem1);
            }
            /*返回查找到的订单*/
            return orderItems;
        } catch (SQLException e) {
            e.printStackTrace();
        }finally {
            try {
                JdbcUtils.close(conn,stat,resultSet);
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
        return null;
    }

    @Override
    public List<OrderItem> findObjByPage(OrderItem orderItem, int pageNum, int pageSize)  {
        int start = (pageNum-1)*pageSize;
        List<OrderItem> orderItems = new ArrayList<>();
        StringBuilder sb = new StringBuilder("select * from order_item where 1 = 1 ");

        if (orderItem.getId()!= 0){
            sb.append(" and id = "+orderItem.getId());
        }
        if (orderItem.getUser_id()!=0){
            sb.append(" and user_id = "+orderItem.getUser_id());
        }
        if (orderItem.getBook_id()!=0){
            sb.append(" and book_id = "+orderItem.getBook_id());
        }
        if (orderItem.getPrice_sum()!=0){
            sb.append(" and price_sum = "+orderItem.getPrice_sum());
        }
        sb.append(" limit "+ start +","+pageSize);
        Connection conn = null;
        Statement stat = null;
        ResultSet resultSet =null;
        try {
            conn= JdbcUtils.getConnection();
            stat = conn.createStatement();
            resultSet = stat.executeQuery(sb.toString());

            while (resultSet.next()){
                OrderItem orderItem1 = new OrderItem();
                orderItem1.setId(resultSet.getInt("id"));
                orderItem1.setUser_id(resultSet.getInt("user_id"));
                orderItem1.setBook_id(resultSet.getInt("book_id"));
                orderItem1.setOrder_id(resultSet.getInt("order_id"));
                orderItem1.setPrice_sum(resultSet.getDouble("price_sum"));
                orderItems.add(orderItem1);
            }
            /*返回分页查找到的订单*/
            return orderItems;
        } catch (SQLException e) {
            e.printStackTrace();
        }
        finally {
            try {
                JdbcUtils.close(conn,stat,resultSet);
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
        return null;
    }
}
